﻿package com.ishuangmu.service.impl;


import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import com.ishuangmu.entity.ArcType;
import com.ishuangmu.entity.Article;
import com.ishuangmu.entity.Link;
import com.ishuangmu.service.ArcTypeService;
import com.ishuangmu.service.ArticleService;
import com.ishuangmu.service.LinkService;


/**
 * 初始化组件 
 * @author Lin
 *
 */
@Component("initComponent")
public class InitComponent implements ServletContextListener,ApplicationContextAware{

	private static ApplicationContext applicationContext;
	
	public void refreshSystem(ServletContext application){
		System.out.println("1111111111111111111111111111111111111111");
		ArcTypeService arcTypeService=(ArcTypeService) applicationContext.getBean("arcTypeService");
		System.out.println("2222222222222222222222222222222222222222222");
		List<ArcType> arcTypeList=arcTypeService.list(null); // 查询所有帖子类型
		application.setAttribute("arcTypeList", arcTypeList); 
		System.out.println("333132222222222222222222222222222222222222222222");
		
		LinkService linkService=(LinkService) applicationContext.getBean("linkService");
		List<Link> linkList=linkService.list(null); // 查询所有友情链接
		application.setAttribute("linkList", linkList);
		
		ArticleService articleService=(ArticleService) applicationContext.getBean("articleService");
		List<Article> hotArticleList=articleService.getHot(); // 获取热门帖子
		application.setAttribute("hotArticleList", hotArticleList);
		System.out.println("333333333333333333333333333");

	}
	
	public void contextInitialized(ServletContextEvent servletContextEvent) {
		ServletContext application=servletContextEvent.getServletContext();
		refreshSystem(application);
	}

	public void contextDestroyed(ServletContextEvent sce) {
		// TODO Auto-generated method stub
		
	}

	
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext=applicationContext;
	}
}
